package com.example.blogother.controller;

import com.example.blogother.mapper.UserinfoMapper;
import com.example.blogother.util.ImageShuiUtil;
import com.xpd.blog.bean.Result;
import com.xpd.blog.bean.Userinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.Console;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/common")
@Slf4j
public class FileController<T> {
    @Autowired
    UserinfoMapper userinfoMapper;
    @Value("${reggie.path}")
    private String basePath;


    @PostMapping("/upload")
    public Result upload(@RequestParam int usid, MultipartFile file) throws IOException {
        log.info("上传文件{}", file.toString());

        //	封装文件名称，防止名称重复
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.indexOf('.'));
        String fileName = UUID.randomUUID().toString();
        //  判断是否存在basePath,如果不存在，先创建
        File dir = new File(basePath);
        if (!dir.exists()) {
            //  目录不存在的话，创建目录
            dir.mkdirs();
        }
        file.transferTo(new File(basePath + fileName + suffix));
        String pathFile = dir  +"/"+fileName+".jpg";
        File inputFile=new File(pathFile);
        File outputFile=new File(pathFile);
        Userinfo userinfo = userinfoMapper.selectById(usid);
        ImageShuiUtil.addTextWatermark(userinfo.getUname(),inputFile,outputFile);
        return new Result(1,"上传成功","image/"+fileName + suffix);
    }

    @PostMapping("/uploads")
    public Result uploads( MultipartFile file) throws IOException {
        log.info("上传文件{}", file.toString());

        //	封装文件名称，防止名称重复
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.indexOf('.'));
        String fileName = UUID.randomUUID().toString();
        //  判断是否存在basePath,如果不存在，先创建
        File dir = new File(basePath);
        if (!dir.exists()) {
            //  目录不存在的话，创建目录
            dir.mkdirs();
        }
        file.transferTo(new File(basePath + fileName + suffix));
        String pathFile = dir  +"/"+fileName+".jpg";
        File inputFile=new File(pathFile);
        File outputFile=new File(pathFile);
       /* Userinfo userinfo = userinfoMapper.selectById(usid);
        ImageShuiUtil.addTextWatermark(userinfo.getUname(),inputFile,outputFile);*/
        return new Result(1,"上传成功","image/"+fileName + suffix);
    }

    /**
     * 文件下载
     *
     * @param name     需要下载的文件名
     * @param response 响应前端，回显图片
     */
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) throws IOException {
        //输入流读取目标文件
        FileInputStream inputStream = new FileInputStream(basePath + name);
        //输出流回显数据
        ServletOutputStream outputStream = response.getOutputStream();
        //设置响应类型
        response.setContentType("image/jpeg");
        int len = 0;
        byte[] bytes = new byte[1024];
        //以bytes大小读取文件
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
            outputStream.flush();
        }
        outputStream.close();
        inputStream.close();
    }


}